<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal"  ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatchTemplate()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                  	  <tbody id="dataLists" >

			                      </tbody>
			                  </table>
						<!--	<div th:insert="/admin/page.html"></div>-->
			                  <!--数据列表/-->

                        </div>
                        <!-- 数据表格 /-->
                        
                     </div>
                    <!-- /.box-body -->
<!-- 编辑窗口 -->
	<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型" name="name" id="name">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select name="brand" id="brand" style="width: 100%" multiple>
							<option th:each="brand :${brandList}" th:value="${brand.id}" >[[${brand.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select name="specn" id="specn" style="width: 100%" multiple>
							<option th:each="spec :${specList}" th:value="${spec.id}" >[[${spec.specName}]]</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody >
								<tr >
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" id="customAttributeItems" name="customAttributeItems"></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="saveTemplate()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 回显窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModalLabel">商品类型模板编辑1</h3>
			  </div>
			  <input  type="hidden" class="form-control" placeholder="商品ID" name="id" id="updateId">
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" placeholder="商品类型" name="name" id="updateName">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select name="brand" id="updateBrand" style="width: 100%" multiple>
								  <option th:each="brand :${brandList}" th:value="${brand.id}" >[[${brand.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select name="specn" id="updateSpecn" style="width: 100%" multiple>
								  <option th:each="spec :${specList}" th:value="${spec.id}" >[[${spec.specName}]]</option>
							  </select>

						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性" ><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody  id="custom">
								 <!-- <tr >
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称"   id="customAttributeItems1"  name="customAttributeItems"></td>
									  <td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>-->
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTemplate()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
    
</body>
</html>
<script type="text/javascript">
	$(function () {
		showTempLateList();
		$("#brand").select2();
		$("#specn").select2();
		$("#updateBrand").select2();
		$("#updateSpecn").select2();
	})
//show
	function showTempLateList() {
		$.ajax({
			url: "/template/getTempLateList",
			type: "post",
			dataType: "json",
			async: true,
			data:{pageNumber:$("[name=pageNumber]").val(),pageSize:$("[name=pageSize]").val()},
			success: function (obj) {
			/*	buildPage(obj.data)*/
				var result = obj.data;
				var datas = '';
				for (let i = 0; i < result.length; i++) {
					var brandIds = [];
					var specIds = [];
					var customAttributeItems = [];

					var jsonBrandIds = JSON.parse(result[i].brandIds);
					console.log(jsonBrandIds)
					for (let j = 0; j < jsonBrandIds.length; j++) {
						brandIds.push(jsonBrandIds[j].text);
					}

					var jsonSpecIds = JSON.parse(result[i].specIds);
					console.log(jsonSpecIds)
					for (let k = 0; k < jsonSpecIds.length; k++) {
						specIds.push(jsonSpecIds[k].text)
					}
					var jsoncustomAttributeItems = JSON.parse(result[i].customAttributeItems);
					console.log(jsoncustomAttributeItems)
					for (let k = 0; k < jsoncustomAttributeItems.length; k++) {
						customAttributeItems.push(jsoncustomAttributeItems[k].text)
					}
					datas += '<tr>';
					datas += '<td><input  type="checkbox" name="box" value="' + result[i].id + '"></td>';
					datas += '<td>' + result[i].id + '</td>';
					datas += '<td>' + result[i].name + '</td>';
					datas += '<td>' + brandIds + '</td>';
					datas += '<td>' + specIds + '</td>';
					datas += '<td>'+customAttributeItems+'</td>';
					datas += '<td class="text-center">';
					datas += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toUpdateTemplate(' + result[i].id + ')"  >修改</button>';
					datas += '</td>';
					datas += '</tr>';

				}
				$("#dataLists").html(datas);
			},
			error: function (datas) {
				toastr.error("品牌页面加载失败");
			}
		})
	}
//save
	function saveTemplate() {
		var brandOptions = $("#brand").select2('data');
		var brandList = [];
			$(brandOptions).each(function (i,e){
				brandList.push({"id":e.id,"text":e.text})
			})
			var brandListJson = JSON.stringify(brandList);

			var specOptions = $("#specn").select2('data');
			var specList = [];
			$(specOptions).each(function (i,e){
				specList.push({"id":e.id,"text":e.text})
			})
			var specListJson = JSON.stringify(specList);

			var templateOptions = $("#customAttributeItems");
			var templateList = [];
			for (let i = 0; i < templateOptions.length; i++) {
				templateList.push({"text":templateOptions[i].value});
			}
			var templateListJson = JSON.stringify(templateList);

			$.ajax({
				url: "/template/saveTemplate",
				type: "post",
				dataType: "json",
				async: true,
				data:{name:$("#name").val(),customAttributeItems:templateListJson,brandIds:brandListJson,specIds:specListJson},
				success: function (result) {
					if(result){
						alert("增加成功")
						window.location.reload()
					}else{
						alert("增加失败")
					}
				},
				error: function (datas) {
					alert("规格页面加载失败");
				}
			})
	}
//updateReverse
	function toUpdateTemplate(id) {
		$.ajax({
			url: "/template/toUpdateTemplate/"+id,
			type: "post",
			dataType: "json",
			async: false,
			success:function (obj){
				var result = obj.data;
				//type
				$("#updateId").val(result.id)
				$("#updateName").val(result.name)
				//brand
				var jsonBrandIds = JSON.parse(result.brandIds);
				var ids = [];
				console.log(jsonBrandIds)
				for (let j = 0; j < jsonBrandIds.length; j++) {
					ids.push(jsonBrandIds[j].id);
				}
				$("#updateBrand").val(ids).trigger("change");
				//spec
				var jsonSpecIds = JSON.parse(result.specIds);
				var ids = [];
				console.log(jsonSpecIds)
				for (let j = 0; j < jsonSpecIds.length; j++) {
					ids.push(jsonSpecIds[j].id);
				}
				$("#updateSpecn").val(ids).trigger("change");
				//custom
				var jsonCustoms = JSON.parse(result.customAttributeItems);
				var html = "";
				$(jsonCustoms).each(function (i, e) {
					html += "<tr>";
					html += "<td><input type=\"checkbox\" class=\"icheckbox_square-blue\" ></td>";
					html += "<td><input type='text' value='" + e.text + "' id='customAttributeItems1' name='customAttributeItems' class='form-control' placeholder='属性名称' ></td>";
					html += "<td><button type='button' class='btn btn-default' title='删除' ><i class='fa fa-trash-o'></i> 删除</button></td>";
					html += "</tr>";
				});
				$("#custom").html(html);

			},
			error:function (result){
				alert("加载失败,请联系管理员")

			},
		})
	}
//update
	function updateTemplate() {
		var brandOptions = $("#updateBrand").select2('data');
		var brandList = [];
		$(brandOptions).each(function (i,e){
			brandList.push({"id":e.id,"text":e.text})
		})
		var brandListJson = JSON.stringify(brandList);

		var specOptions = $("#updateSpecn").select2('data');
		var specList = [];
		$(specOptions).each(function (i,e){
			specList.push({"id":e.id,"text":e.text})
		})
		var specListJson = JSON.stringify(specList);

		var templateOptions = $("#customAttributeItems1");
		var templateList = [];
		/*$(templateOptions).each(function (i,e){
            templateList.push({"text":e.text})
        })*/

		for (let i = 0; i < templateOptions.length; i++) {
			templateList.push({"text":templateOptions[i].value});
		}
		var templateListJson = JSON.stringify(templateList);

		$.ajax({
			url: "/template/updateTemplate",
			type: "post",
			dataType: "json",
			async: true,
			data:{name:$("#updateName").val(),id:$("#updateId").val(),customAttributeItems:templateListJson,brandIds:brandListJson,specIds:specListJson},
			success: function (result) {
				if(result){
					alert("修改成功")
					window.location.reload()
				}else{
					alert("修改失败")
				}
			},
			error: function (datas) {
				alert("规格页面加载失败");
			}
		})
	}
//delete
	function deleteBatchTemplate() {
		var ids=[];
		$("[name=box]:checked").each(function (i,e){
			ids.push($(e).val());
		})
		$.ajax({
			url: "/template/deleteBatchTemplate/"+ids,
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				alert(result.message);
				location.reload();
			},
			error: function (result) {
				alert("系统异常,请联系管理员");
			}
		})
	}

</script>